﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;


namespace WebSite.admin.system
{
    public partial class navigator : BaseAdmin
    {

        readonly DALNavigator theNavigator = new DALNavigator();
        tbNavigator oNavigator;
        
        protected void Page_Load(object sender, EventArgs e)
        {
           
       
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.ProductClass);
                this.DeleletNavigator();
                this.BindGrid();
             
            }
        }
        private void BindGrid()
        {
            string sUpId = "0";
            if (Request.QueryString["pid"] != null)
            {
                sUpId = Request.QueryString["pid"];  
            }
            DataTable table = theNavigator.GetList(string.Format(" navigatorName like '%{0}%' and parentId = {1}", this.txtKeywords.Text.Trim(), sUpId)).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };

        }



        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;

        }

        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theNavigator.GetList("").Tables[0];
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        void DeleletNavigator()
        {
            if (Request.QueryString["action"] == "del")
            {
                oNavigator = theNavigator.GetModel(int.Parse(Request.QueryString["id"]));
                if (oNavigator != null && oNavigator.AllowDeleted)
                {
                    theNavigator.Delete(int.Parse(Request.QueryString["id"]));
                    ExtAspNet.Alert.Show("删除栏目成功！");
                }
                else
                {
                    ExtAspNet.Alert.Show("请先更新栏目的删除状态！");
                }
            }
        }

        public string HeaderHtml
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"] != "0")
                {

                    oNavigator = this.theNavigator.GetModel(int.Parse(Request.QueryString["id"]));
                    if (oNavigator != null)
                    {
                        string sHead = string.Format(" <div  class=\"navgater\"><img src=\"../images/module.gif\" width=\"20\" height=\"22\" alt=\"\"/> "
                                                  + " 栏目名称：{0} "
                                                  + " <a style=\" display:block; float:right;\" href=\"javascript:location.href='navigator.aspx?id={1}';\" ><img src=\"../images/history.gif\" border=\"0\" /></a>"
                                                  + " </div>"
                                                  , oNavigator.navigatorName
                                                  , oNavigator.parentId.ToString()
                                                  );
                        return sHead;
                    }
                }
                return string.Empty;

            }


        }

        protected void btnBrachDeleted_Click1(object sender, EventArgs e)
        {

            int selectedCount = this.Grid1.SelectedRowIndexArray.Length;
            StringBuilder sbNavigator = new StringBuilder();
            for (int i = 0; i < selectedCount; i++)
            {
                int rowIndex = this.Grid1.SelectedRowIndexArray[i];
                if (i == selectedCount - 1)
                {
                    sbNavigator.Append(this.Grid1.DataKeys[rowIndex][0].ToString());
                }
                else
                {
                    sbNavigator.Append(this.Grid1.DataKeys[rowIndex][0].ToString() + ",");
                }
            }
            if (sbNavigator.Length > 0)
            {
                 theNavigator.DeleteList(sbNavigator.ToString());
            }
            ExtAspNet.Alert.Show("批量删除栏目成功！");
            this.BindGrid();
        }

      

    }
}